﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    
    public partial class Form1 : Form
    {
        CulculOperation Calc = new CulculOperation();
        private bool isclear = false,issep=false;        
        private byte Operation = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0")||(isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "1";
            isclear = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "5";
            isclear = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "2";
            isclear = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "3";
            isclear = false;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            
            Calc.key=1;
            Calc.Setmem(textBox1.Text);
            Calc.ischange = true;
            isclear = true;
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "4";
            isclear = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "6";
            isclear = false;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "7";
            isclear = false;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "8";
            isclear = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "9";
            isclear = false;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + "0";
            isclear = false;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Calc.Setmem(textBox1.Text);
            Calc.Oper(Calc.key);
            textBox1.Text = Convert.ToString(Calc.rez);

        }

        private void button12_Click(object sender, EventArgs e)
        {
            Calc.key = 2;
            Calc.Setmem(textBox1.Text);
            Calc.ischange = true;
            isclear = true;
            
            
        }

        private void button16_Click(object sender, EventArgs e)
        {
            Calc.key = 5;
            Calc.ischange = false;
            isclear = false;
            Calc.Setmem(textBox1.Text);
            Calc.Oper(Calc.key);
            textBox1.Text = Convert.ToString(Calc.rez);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            Calc.key = 3;
            Calc.Setmem(textBox1.Text);
            Calc.ischange = true;
            isclear = true;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Calc.key = 4;
            Calc.Setmem(textBox1.Text);
            Calc.ischange = true;
            isclear = true;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            Calc.key = 6;
            Calc.ischange = false;
            isclear = false;
            Calc.Setmem(textBox1.Text);
            Calc.Oper(Calc.key);
            textBox1.Text = Convert.ToString(Calc.rez);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            Calc.key = 7;
            Calc.ischange = false;
            isclear = false;
            Calc.Setmem(textBox1.Text);
            Calc.Oper(Calc.key);
            textBox1.Text = Convert.ToString(Calc.rez);
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (!issep)
            {
                textBox1.Text += ",";
                issep = true;
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            Calc.ClearVal();
            issep = false;
            isclear = false;
            textBox1.Text = "0";

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
                    
         }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            
            switch (e.KeyCode)
            {
                case Keys.NumPad1:
                    
                    NumPress("1");
                    break;
                case Keys.NumPad2: NumPress("2");
                    break;
                case Keys.NumPad3: NumPress("3");
                    break;
                case Keys.NumPad4: NumPress("4");
                    break;
                case Keys.NumPad5: NumPress("5");
                    break;
                case Keys.NumPad6: NumPress("6");
                    break;
                case Keys.NumPad7: NumPress("7");
                    break;
                case Keys.NumPad8: NumPress("8");
                    break;
                case Keys.NumPad9: NumPress("9");
                    break;
                case Keys.NumPad0: NumPress("0");
                    break;
                case Keys.Add:
                    Calc.key=1;
                    Calc.Setmem(textBox1.Text);
                    Calc.ischange = true;
                    isclear = true;
                    break;
                case Keys.Subtract:
                    Calc.key = 2;
                    Calc.Setmem(textBox1.Text);
                    Calc.ischange = true;
                    isclear = true;
                    break;
                case Keys.Multiply:
                    Calc.key = 3;
                    Calc.Setmem(textBox1.Text);
                    Calc.ischange = true;
                    isclear = true;
                    break;
                case Keys.Divide:
                    Calc.key = 4;
                    Calc.Setmem(textBox1.Text);
                    Calc.ischange = true;
                    isclear = true;
                    break;
                case Keys.Decimal:
                    if (!issep)
                    {
                        textBox1.Text += ",";
                        issep = true;
                    }
                    break;
                default:
                break;

            }   
               
        }
        public void NumPress(string s)
        {
            if ((textBox1.Text == "0") || (isclear)) textBox1.Text = "";
            textBox1.Text = textBox1.Text + s;
            isclear = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }
    }

    public class CulculOperation
    {
        public double mem=0,rez=0;
        public byte key = 0;
        public bool ischange = false;
        public void Oper(byte Key)
        {
            switch (Key)
            {
                case 1:
                    rez = rez + mem;
                    mem = 0;
                    key = 0;
                    ischange = false;
                    break;

                case 2:
                    rez = rez - mem;
                    mem = 0;
                    key = 0;
                    ischange = false;
                break;
                case 3:
                    rez = rez * mem;
                    mem = 0;
                    key = 0;
                    ischange = false;
                break;
                case 4:
                if (mem != 0)
                {
                    rez = rez / mem;
                    mem = 0;
                }
                    key = 0;
                    ischange = false;

                break;
                case 5:
                    rez = -1 * rez;
                    key = 0;
                break;
                case 6:
                if (rez != 0)
                    rez = 1 / rez;
                    key = 0;
                break;
                case 7:
                    if (rez > 0)
                        rez = Math.Sqrt(rez);
                        key = 0;
                break;
                default:
                break;


            }
            
         }
        public void Setmem(string s)
        {
            if (ischange)
                mem = Convert.ToDouble(s);
            else rez = Convert.ToDouble(s);

         }
        public void ClearVal()
        {
            mem = 0;
            rez = 0;
            key = 0;
            ischange = false;
            
        }

    }

}
